package com.zy.service.bs.impl;

import com.zy.dao.bs.NoticeDao;
import com.zy.entity.bs.Notice;
import com.zy.entity.enums.StatusEnum;
import com.zy.pojo.exceptions.ServiceException;
import com.zy.pojo.query.bs.NoticeQuery;
import com.zy.pojo.vos.common.PageVo;
import com.zy.service.base.impl.BaseServiceImpl;
import com.zy.service.bs.NoticeService;
import com.zy.service.fileutil.FileUploadService;
import com.zy.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;
import java.util.Objects;

@Service("noticeService")
public class NoticeServiceImpl extends BaseServiceImpl<Notice> implements NoticeService {

	@Autowired
	private NoticeDao noticeDao;
	@Autowired
	private FileUploadService fileUploadService;
	@Value("${fileUploadPath}")
	private String fileUploadPath;

	@Override
	public PageVo pageNotice(NoticeQuery noticeQuery, Integer start, Integer limit) {
		List<Notice> noticeList = this.noticeDao.listNotice(noticeQuery, start, limit);
		noticeList.forEach((Notice notice)->{
			if(notice.getNoticeImg()!=null){
				notice.setNoticeImgPath(fileUploadPath + notice.getNoticeImg());
			}
		});
		return new PageVo(start, limit, this.noticeDao.countNotice(noticeQuery), noticeList);
	}

	@Override
	public void saveNotice(Notice notice) {
		long time = DateUtil.getTime();
		if(notice.getNoticeImgs().get(0).getSize() == 0){
			throw new ServiceException("公告图片不能为空！");
		}
		String noticeImg;
		try {
			noticeImg = fileUploadService.multipleUpload(notice.getNoticeImgs(), "fileUploadPath");
		} catch (IOException e) {
			e.printStackTrace();
			throw new ServiceException("公告图片上传失败！");
		}
		notice.setNoticeImg(noticeImg);
		notice.setCreateTime(time);
		notice.setModifyTime(time);
		notice.setStatus(StatusEnum.NORMAL.getValue());
		this.noticeDao.save(notice);
	}
	
	@Override
	public void updateNotice(Notice notice) {
		if(notice.getNoticeImgs().get(0).getSize() > 0){
			Notice notice2 = this.noticeDao.getByPrimaryKey(notice.getId());
			try {
				if(Objects.nonNull(notice2.getNoticeImg())){
					fileUploadService.deleteImage(notice2.getNoticeImg());
				}
			} catch (IOException e1) {
				e1.printStackTrace();
				throw new ServiceException(fileUploadPath+"未找到图片");
			}

			String fileName;
			try {
				fileName = fileUploadService.multipleUpload(notice.getNoticeImgs(), "fileUploadPath");
			} catch (IOException e) {
				e.printStackTrace();
				throw new ServiceException("合作、赞助商图片修改失败！");
			}
			notice.setNoticeImg(fileName);
		}
		notice.setModifyTime(DateUtil.getTime());
		this.noticeDao.updateByPrimaryKeySelective(notice);
	}

	@Override
	public void removeNotice(long id) {
		try {
			Notice notice = new Notice();
			notice.setId(id);
			notice = this.noticeDao.get(notice);
			if(Objects.nonNull(notice.getNoticeImg())){
				this.fileUploadService.deleteImage(notice.getNoticeImg());
			}
			this.noticeDao.removeByPrimaryKey(id);
		} catch (Exception e) {
			log.info(e.getMessage(),e);
			Notice notice = new Notice();
			notice.setId(id);
			notice.setModifyTime(DateUtil.getTime());
			notice.setStatus(StatusEnum.DELETE.getValue());
			this.noticeDao.updateByPrimaryKeySelective(notice);
		}
	}

	@Override
	public void recover(long id) {
		Notice notice = new Notice();
		notice.setId(id);
		notice.setModifyTime(DateUtil.getTime());
		notice.setStatus(StatusEnum.NORMAL.getValue());
		this.noticeDao.updateByPrimaryKeySelective(notice);
	}

}
